package logic

import (
	"com-man-sys/comrpc/comrpc"
	"com-man-sys/comrpc/internal/svc"
	"com-man-sys/models/mysql"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShangAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShangAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShangAddLogic {
	return &ShangAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShangAddLogic) ShangAdd(in *comrpc.ShangAddRequest) (*comrpc.ShangAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.SHangList(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	users := &mysql.Shang{
		Name:    in.Name,
		Price:   in.Price,
		Type:    int(in.Type),
		Address: in.Address,
		UserId:  int(in.UserId),
	}
	err = users.ShangAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &comrpc.ShangAddResponse{
		ShangId: int64(users.ID),
	}, nil
}
